<?php

namespace apiend\modules\shop\models;

use Yii;
use common\logics\ExpressCalculator;

/**
 * 商品运费计算模型
 * 
 * @author lisen<lisen@163.com>
 * @since 2.0
 */
class SpuExpress extends \common\base\Model {

    public $product_id;
    public $area_code;
    public $num = 1;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['product_id', 'area_code'], 'required'],
            [['area_code'], 'string'],
            [['product_id', 'num'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function reckon() {
        if (!$this->validate()) {
            return $this->dumpError();
        }
        $expressItems = [
            [
                'product_id' => $this->product_id,
                'num' => $this->num,
            ]
        ];
        $calcexpress = new ExpressCalculator();
        $calcexpress->setAttributes([
            'items' => $expressItems,
            'areacode' => $this->area_code,
        ]);
        return $calcexpress->reckon();
    }

}
